import WKT from 'ol/format/WKT'
import GeoJSON from 'ol/format/GeoJSON'
import TopoJSON from 'ol/format/TopoJSON'
import Feature from 'ol/Feature'
import Point from 'ol/geom/Point'
import LineString from 'ol/geom/LineString'
import Polygon from 'ol/geom/Polygon'
import Circle from 'ol/geom/Circle'
import { config } from './config'
export default {
    /**
     * @param {Obejct} param = {type（数据格式）, data（geojson数据, 【layer（图层）】}
     */
    format(param) {
        let format = null
        let features = []
        switch (param.type.toLowerCase()) {
            case 'wkt':
                format = new WKT()
                break
            case 'geojson': 
                format = new GeoJSON()
                break
            case 'topojson': 
                format = new TopoJSON()
                break
        }
        if (format) {
            features = format.readFeatures(param.data, {
                dataProjection: config.sourceProj,
                featureProjection: config.systemProj
            })
            if (param.layer && features) {
                param.layer.getSource().addFeatures(features)
            }
        }
        console.log('features', features)
        return features
    },
    geom(param) {
        let feature = new Feature()
        if (param.data) {
            let geom = null
            switch (param.type.toLowerCase()) {
                case 'point': {
                    geom = new Point()(param.data)
                    break
                }
                case 'linestring': {
                    geom = new LineString(param.data)
                    break
                }
                case 'polygon': {
                    geom = new Polygon(param.data)
                    break
                }
                case 'circle': {
                    geom = new Circle(param.data.center, param.data.radius)
                    break
                }
            }
            if (geom) {
                feature = new Feature(geom)
                if (param.layer && feature) {
                    param.layer.getSource().addFeature(feature)
                }
            }
        }
        return feature
    },
    circleWKT(param) {
        const circle = param.feature.getGeometry()
        const geom = Polygon.fromCircle(circle).transform(param.systemProj || config.systemProj, param.sourceProj || config.sourceProj)
        return new WKT().writeGeometry(geom)
    },
    WKT(param) {
        const feature = param.feature.clone()
        const geom = feature.getGeometry().transform(param.systemProj || config.systemProj, param.sourceProj || config.sourceProj)
        return new WKT().writeGeometry(geom)
    },
    // 获取feature的集合图形
    getGeometry(feature) {
        let geom = feature.getGeometry()
        console.log('geom', geom)
    },
    // 清除feature
    removeFeature(layer, feature) {
        if (layer && feature) {
            layer.getSource().removeFeature(feature)
        }
    }
}
// # sourceMappingURL=feature.js.map
